from rest_framework import serializers
from . import models
from basics import models as basics_models


class AutomatedTaskSerializers(serializers.ModelSerializer):
    owner_user_info = serializers.SerializerMethodField(read_only=True)  # 任务所有者
    project_info = serializers.SerializerMethodField(read_only=True)  # 项目表对应字段
    test_person_info = serializers.SerializerMethodField(read_only=True)  # 用例负责人
    module_info = serializers.SerializerMethodField(read_only=True)  # 所属模块
    task_info = serializers.SerializerMethodField(read_only=True)  # 任务信息
    cases_count = serializers.SerializerMethodField(read_only=True)  # 用例数量
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)
    update_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)

    def get_owner_user_info(self,obj):
        myobj = obj.owner_user
        if not myobj:
            return myobj
        return {
            "id": myobj.id,
            "username": myobj.username,
            "name": myobj.name,
            'value': myobj.name if myobj.name else myobj.username,
        }

    def get_task_info(self, obj):  # 获取任务信息
        myobj = obj.task
        if not myobj:
            return myobj
        return {
            'id': myobj.id,
            'name': myobj.name,
            'task': myobj.task,  # 执行函数
            'value': '{name} / {task}'.format(name=myobj.name, task=myobj.task)
        }

    def get_project_info(self, obj):  # 获取表字段的对应方法
        myobj = obj.project
        if not myobj:
            return myobj
        return {
            "id": myobj.id,
            "name": myobj.name,
            "describe": myobj.describe,
            "status": myobj.status,
            'value': myobj.name,
        }

    def get_test_person_info(self, obj):  # 获取表字段的对应方法
        myobj = obj.test_person
        if not myobj:
            return myobj
        return {
            "id": myobj.id,
            "username": myobj.username,
            "name": myobj.name,
            'value': myobj.name if myobj.name else myobj.username,
        }

    def get_module_info(self, obj):  # 获取表字段的对应方法
        myobj = obj.module
        if not myobj:
            return myobj
        return {
            "id": myobj.id,
            'developer': myobj.developer.username,
            'testers': myobj.testers.username,
            "name": myobj.name,
            'value': myobj.name
        }

    def get_cases_count(self,obj):
        select_fields = {
            'test_person': obj.test_person.id if obj.test_person else None,
            'project': obj.project.id if obj.project else None,
            'module': obj.module.id if obj.module else None,
            'property': obj.property,
            'level': obj.level,
            'test_status': obj.test_status,
            'status': obj.status,
        }
        fields_data = {key:select_fields[key] for key in select_fields if select_fields[key]}
        cases_count = basics_models.Cases.objects.filter(**fields_data).count()
        return cases_count


    class Meta:
        model = models.AutomatedTask
        fields = "__all__"  # 代表获取所有字段
        # depth = 1  # 以外键关系向下找1层，数字代表层数
        extra_kwargs = {
            "owner_user":{"write_only": True},
            "test_person": {"write_only": True},
            "module": {"write_only": True},
            "project": {"write_only": True},
            "task":{"write_only": True},
        }


class AutomatedTaskRecordSerializers(serializers.ModelSerializer):
    owner_user_info = serializers.SerializerMethodField(read_only=True)  # 记录所有者
    task_info = serializers.SerializerMethodField(read_only=True)  # 任务信息
    automated_task_info = serializers.SerializerMethodField(read_only=True)  # 收集用例
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)
    update_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)

    def get_owner_user_info(self,obj):
        myobj = obj.owner_user
        if not myobj:
            return myobj
        return {
            "id": myobj.id,
            "username": myobj.username,
            "name": myobj.name,
            'value': myobj.name if myobj.name else myobj.username,
        }

    def get_automated_task_info(self, obj):  # 获取任务信息
        myobj = obj.automated_task
        if not myobj:
            return myobj
        return {
            'id': myobj.id,
            'title': myobj.title,
            'value': myobj.title
        }

    def get_task_info(self, obj):  # 获取任务信息
        myobj = obj.task
        if not myobj:
            return myobj
        return {
            'id': myobj.id,
            'name': myobj.name,
            'task': myobj.task,  # 执行函数
            'value': '{name} / {task}'.format(name=myobj.name, task=myobj.task)
        }

    class Meta:
        model = models.AutomatedTaskRecord
        fields = "__all__"  # 代表获取所有字段
        # depth = 1  # 以外键关系向下找1层，数字代表层数
        extra_kwargs = {
            "owner_user": {"write_only": True},
            "task":{"write_only": True},
            "automated_task":{"write_only": True},
        }
